﻿# // ***************************************************************************
# // 
# // Copyright (c) Scriptimus Ex Machina.  All rights reserved.
# // 
# // Microsoft Deployment Toolkit Support Script
# //
# // File:      ZTIRename-Computer.ps1
# // 
# // Version:   1.00 
# // 
# // Purpose:   Renames a computer
# // 
# // ***************************************************************************

<#
.SYNOPSIS 
Lite-Touch Deployment script to rename a computer

.DESCRIPTION
The ZTIRename-Computer.ps1 script will raname a computer and join a new workgroup.
    
.PARAMETER Name
Specifies the new computername

.PARAMETER WorkgroupName
Specifies the new Workgroup name

.EXAMPLE
%SCRIPTROOT%\ZTIRename-Computer.ps1
Add this line to your add powershell task sequence to take values from the deployment 
properties OSDComputername and Workgroup. Remember to add the task sequence step 
Restart Computer immediately after this step.

.EXAMPLE
ZTIRename-Computer.ps1 -Name PC01 -Workgroup Continuum
This will rename the computer to PC01 and the workgroup to Continuum after a reboot.

.NOTES
Author: Andrew Barnes
14 September 2012 - Creation
23 September 2012 - Documentation
24 February 2013 - Updated workgroup logic

.LINK
Online version: http://scriptimus.wordpress.com
            
#>

[CmdletBinding()]

Param (

    [String]$Name = $TSEnv:OSDComputername,

    [String]$WorkGroupName

)

Begin{

    Write-Host "Begining script"

    #$DebugPreference="Continue"

    Import-Module ZTIUtility

    }

Process{

    Write-Host "Processing Script"

    If ($Name) {

        # Rename the Computer using WMI

        (Get-WmiObject win32_computersystem).rename($Name)

        Write-Host "Computer was renamed to $Name"

    }



    # Process only if the WorkGroupName variable has no value passed to the script.

    If (!($WorkGroupName)) {

        Write-Debug "WorkGroupName variable has no value passed to the script."



        # Process only if the Task Sequence variable JoinWorkgroup has a value.

        If ($TSEnv:JoinWorkgroup) {

            Write-Debug "Task Sequence variable JoinWorkgroup has a value."

            [String]$WorkGroupName = $TSEnv:JoinWorkgroup

        }

    }

        
    # Join a Workgroup

    # Process only if the WorkGroupName variable has a value and it does not equal the current Workgroup name.

    If ($WorkGroupName -and ($WorkGroupName -ne ((Get-WmiObject Win32_ComputerSystem).domain))) {

        Write-Debug "WorkGroupName variable has a value."

        Add-Computer -WorkGroupName $WorkGroupName -WarningAction SilentlyContinue

        Write-Host "Joined the Workgroup $WorkGroupName"

    }

}

End{
    
    Write-Host "End Script"
    
    }